﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionRestaurant
    {
        public void Ajouter(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Restaurants.AddObject(restaurant);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public List<Restaurant> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Restaurants.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Restaurant Chercher(int restaurantid)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Restaurants.Include("Manager").
                        Where(r => r.Id == restaurantid).
                        FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Restaurants.Attach(restaurant);
                    db.ObjectStateManager.ChangeObjectState(restaurant, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer (Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Restaurants.DeleteObject(restaurant);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
